package cn.smbms.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.smbms.pojo.Trolley;
import cn.smbms.pojo.User;
import cn.smbms.service.trolley.TrolleyService;
import cn.smbms.tools.Constants;

import com.alibaba.fastjson.JSON;

@Controller
@RequestMapping(value = "/trolley")
public class TrolleyController {
	@Resource
	private TrolleyService trolleyService;

	// 购物车页面进入
	@RequestMapping(value = "/sys/shopcar.html")
	public String myMeassage(HttpSession session, Model model) {
		User user = new User();
		user.setId(((User) session.getAttribute(Constants.USER_SESSION))
				.getId());
		List<Trolley> troList = trolleyService.getTrolleyList(user.getId());
		model.addAttribute("troList", troList);
		return "shopcar";
	}

	// 购物车添加宝贝，返回json数据
	@RequestMapping(value = "/sys/add", method = RequestMethod.POST)
	@ResponseBody
	public Object add(HttpSession session, @RequestParam String proid,
			@RequestParam String color, @RequestParam String size,
			@RequestParam String num, @RequestParam String count) {
		String colo = "";
		String dest = "";
		if (color != null) {
			Pattern p = Pattern.compile("\\s*|\t|\r|\n");
			Matcher m = p.matcher(color);
			colo = m.replaceAll("");
		}
		if (size != null) {
			Pattern p = Pattern.compile("\\s*|\t|\r|\n");
			Matcher m = p.matcher(size);
			dest = m.replaceAll("");
		}
		HashMap<String, String> resultMap = new HashMap<String, String>();
		User user = ((User) session.getAttribute(Constants.USER_SESSION));
		if (user != null) {
			if (trolleyService
					.getTrolley(Integer.parseInt(proid), user.getId()) > 0) {

				int i = trolleyService.trolleySave(Integer.parseInt(proid),
						user.getId());
				if (i > 0) {
					System.out.println("添加成功");
					resultMap.put("add", "true");
				} else {
					System.out.println("添加失败");
					resultMap.put("add", "false");
				}
			} else {
				double coun = Float.parseFloat(count);
				String id = user.getId() + "";
				int i = trolleyService.trolleyadd(id, proid, num, coun, colo,
						dest);
				if (i > 0) {
					System.out.println("添加成功");
					resultMap.put("add", "true");
				} else {
					System.out.println("添加失败");
					resultMap.put("add", "false");
				}
			}

		} else {
			resultMap.put("add", "user");
		}
		return JSON.toJSONString(resultMap);
	}

	// 购物车删除宝贝，返回json数据
	@RequestMapping(value = "/del")
	@ResponseBody
	public Object deltrolley(@RequestParam Integer id) {
		System.out.println("我进来了");
		HashMap<String, String> resultMap = new HashMap<String, String>();
		if (trolleyService.delTrolley(id)) {
			System.out.println("删除成功");
			resultMap.put("delResult", "true");
		} else {
			System.out.println("删除失败");
			resultMap.put("delResult", "false");
		}
		return JSON.toJSONString(resultMap);
	}

	@RequestMapping(value = "modif", method = RequestMethod.GET)
	@ResponseBody
	public Object updateTrolley(@RequestParam Integer id,
			@RequestParam int num, @RequestParam Float count) {
		HashMap<String, String> resultMap = new HashMap<String, String>();
		BigDecimal b = new BigDecimal(String.valueOf(count));
		double d = b.doubleValue();
		Trolley trolley = new Trolley();
		trolley.setId(id);
		trolley.setNum(num);
		trolley.setCount(d);
		if (trolleyService.updateTrolley(trolley)) {
			resultMap.put("updaResult", "true"); // 修改成功
		} else {
			resultMap.put("updaResult", "false"); // 修改成功
		}
		return JSON.toJSONString(resultMap);
	}
}
